import { obj } from "../../index.js";
import dom, { ajax, domg } from "../../init.js";
import { getAllLimit } from "./aritcle.js";
import header from "../../header/eventhandle.js";
import { article, articlelimit, commemt } from "../../../util/interface.js";
import { isarticle } from "../../navigation/navigation.js";
import { toarticle } from "../../navigation/eventhandle.js";

export default {
  async articlelist(e: Event) {
    //获取当前显示文章所有的标题和作者
    let arr1 = [...document.querySelectorAll(".title")];
    let arr2 = [...document.querySelectorAll(".articleauthor")];
    let arr3 = [...document.querySelectorAll(".articlecommentul")];
    let arr4 = [...document.querySelectorAll(".articlecommentlist>a")];

    //如果点击的标题或者评论列表
    //如果点击的是作者就搜索所有的作者
    //如果点击的是评论数量就会打开评论
    if (
      arr1.includes(e.target as HTMLElement) ||
      arr1.includes((e.target as HTMLElement).parentElement as HTMLElement) ||
      arr4.includes((e.target as any).parentElement)
    ) {
      const result = await ajax.get("/article/findpk", {
        id: (this as any).getAttribute("data-index"),
      });
      if ((result as string).includes("Error")) return console.log("出错了");
      const res = JSON.parse(result as string);
      const data = res as articlelimit;
      if (data.msg !== "ok") return alert(data.msg);
      localStorage.setItem("article", JSON.stringify(data.data));
      location.hash = "/MyArticle";
      isarticle();
    } else if (arr2.includes(e.target as HTMLElement)) {
      (dom.herder.findtitleipt as any).value = (
        e.target as HTMLElement
      ).innerText;
      header.findtitle();
    } else if (arr3.includes(e.target as HTMLElement)) {
      const taget = (e.target as HTMLElement).parentElement
        ?.nextElementSibling as HTMLElement;
      if (taget)
        if (!taget.style.height) {
          const len = +(e.target as HTMLElement).innerText * 50;
          taget.style.height = len + "px";
          taget.style.color = "color: rgb(168, 20, 236)";
        } else {
          taget.style.height = "";
          taget.style.color = "";
        }
    }
  },
  async articlelimit(e: Event) {
    if (e.target instanceof HTMLElement) {
      if (e.target.getAttribute("index")) {
        //重复点击一页不发请求
        if (obj.limit.x == e.target.getAttribute("index")) return;
        obj.limit.x = e.target.getAttribute("index") as string;
        getAllLimit(obj.url);
      }
    }
  },
  //点击排序的事件委托
  Top(e: Event) {
    //如果点击的是时间排序
    if (e.target === domg(".timeTop")) {
      setTimeout(() => {
        domg(".timeTop").style.color = "rgb(137 207 139)";
        domg(".browesNumTop").style.color = "#888";
      }, 30);
      //判断当前状态
      if (sessionStorage.getItem("timeTop") == "11") {
        obj.url = "/article/getTimeTop";
        sessionStorage.setItem("timeTop", "22");
        getAllLimit("/article/getTimeTop");
        setTimeout(() => {
          domg(".timeTop").innerText = "时间升序";
        }, 30);
      } else {
        sessionStorage.setItem("timeTop", "11");
        obj.url = "/article/getTimeTop";
        getAllLimit("/article/getTimeTop");
        domg(".timeTop").setAttribute("ist", "12");
        setTimeout(() => {
          domg(".timeTop").innerText = "时间降序";
        }, 30);
      }
    }
    //如果点击的是时间排序
    if (e.target === domg(".browesNumTop")) {
      setTimeout(() => {
        domg(".timeTop").style.color = "#888";
        domg(".browesNumTop").style.color = "rgb(137 207 139)";
      }, 30);
      //判断当前状态
      if (sessionStorage.getItem("browesNumTop") == "11") {
        obj.url = "/article/getBrowesNumTop";
        sessionStorage.setItem("browesNumTop", "22");
        getAllLimit("/article/getBrowesNumTop");
        setTimeout(() => {
          domg(".browesNumTop").innerText = "热度升序";
        }, 30);
      } else {
        sessionStorage.setItem("browesNumTop", "11");
        obj.url = "/article/getBrowesNumTop";
        getAllLimit("/article/getBrowesNumTop");
        setTimeout(() => {
          domg(".browesNumTop").innerText = "热度降序";
        }, 30);
        domg(".timeTop").setAttribute("ist", "11");
      }
    }
  },
};
